<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'misc/image_manipulation_functions.inc.php');


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/image/tcms_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_mod_dbc.class.php');


class TCMS_Image_Upload extends Tangra_Class {
	public static function process_uploaded_image(DB_Connection $dbc,
													$file_properties,
													$uploads_dir,
													TCMS_Settings_Images_DBC $images_settings,
													$site_root_dir,
													$ci_id = 0,
													$current_user = 0
													) {

		$ret = false;

		$target_dir = tangra_normalize_path($uploads_dir).$images_settings->get_upload_path();

		$image_dbc = new TCMS_Image_DBC();
		$image_dbc->set_orig_filename($file_properties['name']);
		$image_dbc->save($dbc); // to get id

		// Generating destination name. Something like 0000000123, i.e. without extension
		$destination_filename = sprintf("%010s", $image_dbc->get_id());
		$image_dbc->set_filename($destination_filename);

		// creating image resource from original image. Will use it later for all transformations
		$image_file_orig_tmp = $file_properties['tmp_name'];
		$image_orig_resource = tangra_create_image_resource_from_file($image_file_orig_tmp);

		if ($image_orig_resource) {
			$watermark_image = $images_settings->get_watermark_image();
			if ($watermark_image != '' && $images_settings->get_watermark_image() != false) {
				$watermark_image_path = tangra_normalize_path($site_root_dir).$watermark_image;

				$watermark_image_resource = tangra_create_image_resource_from_file($watermark_image_path);
			}


			// creating main image
			$main_image = tangra_image_resize_to_max_x_or_y($image_orig_resource, $images_settings->get_to_size_x(), $images_settings->get_to_size_y());
			$main_image_path = $target_dir.$destination_filename.'.jpg';
			if ($watermark_image != '' && $images_settings->get_watermark_image() != false) {
				TCMS_Image_Upload::add_watermark($main_image, $watermark_image_resource, $images_settings->get_watermark_position());
			}
			imagejpeg($main_image, $main_image_path, $images_settings->get_jpg_quality());


			// creating fixed image size
			$bg_colors = TCMS_Image::split_bg_colors($images_settings->get_bgcolor());

			$fixed_image_path = $target_dir.$destination_filename.TCMS_Image::FIXED_SIZE_IMAGE_SUFFIX .'.jpg';
			$fixed_image = tangra_image_resize_to_fixed_size_no_stretch($image_orig_resource,
																		$images_settings->get_to_size_x(),
																		$images_settings->get_to_size_y(),
																		true,
																		true,
																		$bg_colors['red'],
																		$bg_colors['green'],
																		$bg_colors['blue']
																		);



			if ($watermark_image != '' && $images_settings->get_watermark_image() != false) {
				TCMS_Image_Upload::add_watermark($fixed_image['img'], $watermark_image_resource, $images_settings->get_watermark_position(), $fixed_image['offset_x'], $fixed_image['offset_y']);
			}
			imagejpeg($fixed_image['img'], $fixed_image_path, $images_settings->get_jpg_quality());
			$image_dbc->set_size(filesize($fixed_image_path));
			$image_dbc->set_size_x(imagesx($main_image));
			$image_dbc->set_size_y(imagesy($main_image));
			$image_dbc->set_path($images_settings->get_upload_path());

			if ($watermark_image != '' && $images_settings->get_watermark_image() != false) {
				TCMS_Image_Upload::add_watermark($image_orig_resource, $watermark_image_resource, $images_settings->get_watermark_position());
			}

			// Thumbnail 1
			if ($images_settings->get_thumbnail1_size_x() && $images_settings->get_thumbnail1_size_y()) {
				$thumbnail1_image_path = $target_dir.$destination_filename.TCMS_Image::THUMBNAIL1_IMAGE_SUFFIX.'.jpg';
				$thumbnail1_image = tangra_image_resize_to_fixed_size_no_stretch($image_orig_resource,
																			$images_settings->get_thumbnail1_size_x(),
																			$images_settings->get_thumbnail1_size_y(),
																			true,
																			true,
																			$bg_colors['red'],
																			$bg_colors['green'],
																			$bg_colors['blue']
																			);
				imagejpeg($thumbnail1_image['img'], $thumbnail1_image_path, $images_settings->get_jpg_quality());
			}


			// Thumbnail 2
			if ($images_settings->get_thumbnail2_size_x() && $images_settings->get_thumbnail2_size_y()) {
				$thumbnail2_image_path = $target_dir.$destination_filename.TCMS_Image::THUMBNAIL2_IMAGE_SUFFIX.'.jpg';
				$thumbnail2_image = tangra_image_resize_to_fixed_size_no_stretch($image_orig_resource,
																			$images_settings->get_thumbnail2_size_x(),
																			$images_settings->get_thumbnail2_size_y(),
																			true,
																			true,
																			$bg_colors['red'],
																			$bg_colors['green'],
																			$bg_colors['blue']
																			);
				imagejpeg($thumbnail2_image['img'], $thumbnail2_image_path, $images_settings->get_jpg_quality());
			}

			if ($images_settings->get_keep_original()) {
				$target_file = $target_dir.$destination_filename . TCMS_Image::ORIGINAL_IMAGE_SUFFIX . '_' . $file_properties['name'];
				move_uploaded_file($image_file_orig_tmp, $target_file);
			} else {
				unlink($image_file_orig_tmp);
			}

			// actual save
			$image_dbc->save($dbc);
			if ($ci_id) {
				$ci_image = new TCMS_CI_Image_DBC();
				$ci_image->set_image($image_dbc->get_id());
				$ci_image->set_ci($ci_id);
				$ci_image->set_ordering(TCMS_CI_Image_DBC::get_next_ordering($dbc, $ci_id));
				$ci_image->save($dbc);

				$mod = new TCMS_CI_Mod_DBC();
				$mod->set_ci($ci_id);
				$mod->set_modified_by($current_user);
				$mod->save($dbc);
			}
			$ret = $image_dbc->get_id();
		} else {
			throw new Tangra_Module_Exception('Cannot create image resource from '.$image_file_orig_tmp);
		}


		return $ret;
	}


	private static function add_watermark(&$source_image, $watermark_image, $watermark_position, $offset_x = 0, $offset_y = 0) {
		$image_watermark = tangra_create_image_resource_from_file($watermark_image);

		$orig_width = imagesx($source_image);
		$orig_height = imagesy($source_image);

		$w_width = imagesx($watermark_image);
		$w_height = imagesy($watermark_image);

		if ($orig_width > $w_width + 10 && $orig_height > $w_height) {
			switch ($watermark_position) {
				case 1:
					$w_pos_x = 10 + $offset_x;
					$w_pos_y = 10 + $offset_y;
					break;
				case 2:
					$w_pos_x = $orig_width - 10 - $w_width - $offset_x;
					$w_pos_y = 10 + $offset_y;
					break;
				case 3:
					$w_pos_x = $orig_width - 10 - $w_width - $offset_x;
					$w_pos_y = $orig_height - 10 - $w_height - $offset_y;
					break;
				case 4:
					$w_pos_x = ($orig_width - $w_width) / 2;
					$w_pos_y = ($orig_height - $w_height) / 2;
					break;
				case 0:
				default:
					$w_pos_x = 10 + $offset_x;
					$w_pos_y = $orig_height - 10 - $w_height - $offset_y;
					break;
			}

			imagecopyresampled($source_image, $watermark_image, $w_pos_x, $w_pos_y, 0, 0, $w_width, $w_height, $w_width, $w_height);
		}
	}
}